package com.eportal.action.background;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.eportal.action.BaseAction;
import com.eportal.domain.Sysopt;
import com.eportal.domain.Sysroles;
import com.eportal.service.SysRolesService;
import com.framework.jqgrid.JqGridPager;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 角色管理
 * @author chencheng
 *
 */
public class RoleAction extends BaseAction implements ModelDriven<Sysroles> {


    private Map<String, Object> result = new HashMap<String, Object>();
    
    @Autowired
    private SysRolesService sysRolesService;


	/**
     * 角色管理
     */
    public String listView() {
        return "listView";
    }
    
    /**
     * 角色模板管理
     */
    public String listModelView() {
        return SUCCESS;
    }
    
    /**
     * 分页查询
     */
    public void page() throws Exception {
        JqGridPager page = new JqGridPager();
        setPage(getRequest(), page);
        Sysopt  user= (Sysopt)getSession().getAttribute("user");
        int userRole =  (Integer) getSession().getAttribute("userRole");
        String	sql = sqlWhere(user,userRole,1);
        List<Sysroles> roles = sysRolesService.list(page,sql);
        jqGridQueryMap(Sysroles.class, getRequest(), roles, getResponse(), page);
    }

    
    /**
     * sql查询拼装
     * @param userRole 
     * @param ifNeed 1：需要追加到超管角色，0：不需要追加到超管角色
     */
    private String sqlWhere(Sysopt user, int userRole, int ifNeed) {
    	StringBuilder where = new StringBuilder();
    	
    	where.append(" 1=1 ");
    	if(userRole == 1){
    		where.append(" ");
    	}else{//非平台超级管理员,先查出内部管理角色
    		where.append("and rAgtent ='");
    		where.append(user.getAgtCode());
    		where.append("' ");
    		where.append("and rCommunity ='");
    		where.append(user.getCommunityId());
    		where.append("' ");
    		where.append("and isAdmin = '0'");
			where.append(" and sid not in (");
			where.append(user.getRoles());
			where.append(")");
    	}
    	if(ifNeed == 0){
    		return where.toString();
    	}
    	String rname = getRequest().getParameter("rname");//查询条件
    	String rlevel = getRequest().getParameter("rlevel");
    	if (StringUtils.isNotBlank(rname)) {
    		where.append(" and rname like'%"+rname+"%'");
		}
    	if (StringUtils.isNotBlank(rlevel)) {
    		where.append(" and rlevel like'%"+rlevel+"%'");
		}
    	
    	StringBuilder sb = new StringBuilder("select Sysr.* from (");
    	sb.append("select * from Sysroles where ");
    	//加上合作方超级管理员角色
    	if(userRole == 2){
    		where.append(" union all select * from Sysroles  where  isAdmin = '1' and rLevel=1 ");
    	}
    	if(userRole == 3){//加上社区超管角色
        	where.append(" union all select * from Sysroles where rAgtent ='");
        	where.append(user.getAgtCode());
        	where.append("' ");
        	where.append("and isAdmin = '1' and rLevel=2 ");
        }
    	sb.append(where);
    	sb.append(") Sysr");
		return sb.toString();
	}

	/**
     * 角色列表,用于操作员分配列表
     */
    public void list() throws Exception {
    	List<Sysroles> sysroles = getAllRole();
        responseBody(sysroles);
    }


    /**
     * 角色修改
     */
    public String editView() {
        Sysroles role = sysRolesService.get(getModel().getSid());
        int userRole =  (Integer) getSession().getAttribute("userRole");
        getRequest().setAttribute("role", role);
    	getRequest().setAttribute("userRole", userRole);
    	Sysopt  user= (Sysopt)getSession().getAttribute("user");
    	getRequest().setAttribute("rLevel", user.getrLevel());
        return "editView";
    }

    /**
     * 角色修改保存
     * @throws IOException 
     */
    public void update() {
    	Sysroles role = getModel();
    	result = sysRolesService.updateMethod(role);
        responseBody(result);
    }
   

    /**
     * 角色增加
     */
    public String addView() {
    	int userRole =  (Integer) getSession().getAttribute("userRole");
    	getRequest().setAttribute("userRole", userRole);
    	Sysopt  user= (Sysopt)getSession().getAttribute("user");
    	getRequest().setAttribute("rLevel", user.getrLevel());
        return "addView";
    }

    /**
     * 角色新增保存
     * @throws IOException 
     */
    public void add() {
    	Sysroles role = getModel();
    	result = sysRolesService.addMethod(role);
        responseBody(result);
    }
    
    

    /**
     * 获取该用户管理的所有角色列表
     */
    public List<Sysroles> getAllRole(){
    	Sysopt  user= (Sysopt)getSession().getAttribute("user");
    	StringBuilder sb  = new StringBuilder(" where");
    	int userRole =  (Integer) getSession().getAttribute("userRole");
    	String where = sqlWhere(user,userRole,0);
    	sb.append(where);
    	List<Sysroles> sysroles = sysRolesService.list(sb.toString());
    	return sysroles;
    }
    
   

	/**
     * 角色删除
     */
    public void delete() {
    	String param_ids = getRequest().getParameter("ids");
    	result = sysRolesService.deleteMethod(param_ids);
        responseBody(result);
    }
    

    private Sysroles role = new Sysroles();

    @Override
    public Sysroles getModel() {
        return role;
    }
}
